<?php

namespace app\api\controller;

use app\service\WeChatMnpService;
use support\Db;
use support\Log;

class WechatMp extends BaseApi
{
    public function wxService()
    {

        $server = (new WeChatMnpService())->getServer();

        //微信图片安全检测
        $server->addEventListener('wxa_media_check', function ($message, \Closure $next) {
            if ($message["errcode"] === "0") {
                if ($message["detail"]["suggest"] == 'pass') {
                    Db::table("media_check")->where("trace_id", $message["trace_id"])->update(['is_risky' => $message['detail']["suggest"]]);
                } else {
                    $postId = Db::table("media_check")->where("trace_id", $message["trace_id"])->value("post_id");
                    Db::table("post")->where("id", $postId)->update(["media" => []]);
                }
            }
        });

        $response = $server->serve();

        return response($response->getBody()->getContents(), $response->getStatusCode(), $response->getHeaders());
    }
}